<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Tags as TagsModel;

class Tags extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //分类列表
        $tagsDatas = TagsModel::all(function ($query) {
            $query->order('tid', 'asc');
        });
        return view('', compact('tagsDatas'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $datas = $request->post('tags');
        $datas = explode('|', $datas);
        //定义筛选后的数据
        $newDatas = [];
        $TagsModel = new TagsModel;
        //查询重复标签
        //查出所有标签数据进行对比筛选
        $tagAll = collection($TagsModel->all())->toArray();
        $tagAllArray = [];
        foreach ($tagAll as $tag) {
            $tagAllArray[] = $tag['tname'];
        }
        //进行标签筛选
        foreach ($datas as $data) {
            if (trim($data) != '') {
                //如果匹配到说明重复 直接忽略
                if (in_array(trim($data), $tagAllArray)) {
                    continue;
                }
                $newDatas[] = $data;
            }
        }
        //插入标签数据
        foreach ($newDatas as $data) {
            if (trim($data) != '') {
                $TagsModel = new TagsModel;
                $TagsModel->data([
                    'tname' => trim($data)
                ]);
                $TagsModel->save();
            }
        }
        $this->success('添加成功', '/admin/tags');
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $data = TagsModel::get($id);
        return view('', compact('data'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->post();
        if ($data['tname'] != '') {
            //查询是否有存在的标签，存在的则提示错误
            $isRepeat = TagsModel::get(['tname' => $data['tname']]);
            if ($isRepeat) {
                $this->error('标签名已存在，请输入新标签名');
            }
            unset($data['_method']);
            $res = TagsModel::where('tid', $id)->update($data);
            if ($res) {
                $this->success('编辑成功', '/admin/tags');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $this->error('不能为空');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (TagsModel::where('tid', $id)->delete()) {
            $res['status'] = '1';
            $res['msg'] = '删除成功';
        } else {
            $res['status'] = '0';
            $res['msg'] = '删除失败';
        }
        return $res;

    }
}
